#include "blood_oxygen.h"
#include "display_ctrl.h"
#include "platform_assert.h"
#include "lfs_file_manager_auto.h"
#include "rtc.h"
#include "datetime.h"
#include "blecom.h"
#include "battery_ctrl.h"
#include "sport_his.h"
#include "display_interface_240x240.h"
#include "datetime.h"
#include "heart_rate_interface.h"
#include "font/num_font.h"
#include "view_common.h"
#include "view.h"
#include "times/view_times.h"
#include "times/view_time_0.h"
#include "sports_mode.h"
#include "app_trace.h"
#include "blood_oxygen.inc"
//#include "Heart_rate_additional.h"
#define LOG(fmt, ...) app_trace_log(fmt "\r\n", ##__VA_ARGS__)

int draw_num_with_screen(int number, int num_length, int x, int y, int num_width, int num_spacing, uint8_t const *const *num_TAB)
{
//    int err = {0};
    int draw_x = 0;
    int draw_y = 0;
    if (num_length % 2 == 1)
    {
        draw_x = x + (num_width / 2);
        draw_y = y;
    }
    else if (num_length % 2 == 0)
    {
        draw_x = x + (num_spacing / 2);
        draw_y = y;
    }

    if (num_length == 1)
    {
        draw_x -= num_width;
        disp_draw_user_image(draw_x, draw_y, num_TAB[number]);
    }

    draw_x += (num_length - 1) * ((num_width + num_spacing) / 2);
    for (int i = 0; i < num_length; i++)
    {
        disp_draw_user_image(draw_x, draw_y, num_TAB[number]);
        draw_x -= (num_width + num_spacing);
        number /= 10;
    }

    return 0;
}

static void view_blood_oxygen_animation(void)
{
    int rect_height = 3;
    int rect_width = 29;
    int rect_height_spacing = 3;
    int x = 8;
    int y = 212;

    for (int i = 0; i < 28; i++)
    {
        disp_draw_rect_color(x, y, x + rect_width, y + rect_height, RGB565_COLOR_YELLO);
        y -= (rect_height + rect_height_spacing);
    }
}

static void view_blood_oxygen_heart_reat(void)
{
    disp_draw_user_image(9, 5, img_blood_oxygen_heart_reat);
}

static void view_blood_oxygen_value(void)
{
    disp_draw_user_image(48, 38, img_blood_oxygen_icon);
    if (disp_ctrl.com_data.i_temp[1] == 0)
    {
        int x = 66;
        int y = 77;
        disp_draw_user_image(x, y, img_blood_oxygen_line);
        x += 4 + 22;
        disp_draw_user_image(x, y, img_blood_oxygen_line);
        return;
    }
    int length = get_num_digit(disp_ctrl.com_data.i_temp[1]);
    draw_num_with_screen(disp_ctrl.com_data.i_temp[1], length, 69, 77, 22, 4, img_blood_oxygen_num_TAB);
}

static void view_blood_oxygen_heart_reat_value(void)
{
    disp_draw_user_image(48, 141, img_blood_oxygen_heart_reat_icon1);
    if (disp_ctrl.com_data.i_temp[1] == 0)
    {
        int x = 66;
        int y = 179;
        disp_draw_user_image(x, y, img_blood_oxygen_line);
        x += 4 + 22;
        disp_draw_user_image(x, y, img_blood_oxygen_line);
        return;
    }
    int length = get_num_digit(disp_ctrl.com_data.i_temp[0]);
    draw_num_with_screen(disp_ctrl.com_data.i_temp[0], length, 69, 179, 22, 4, img_blood_oxygen_num_TAB);
}


void view_blood_oxygen_display(int view)
{
    if (disp_ctrl.loop_cnt == 0)
    {
        if (disp_ctrl.auto_refresh_count % 10 == 0)
        {
            // heart_rate_additional_generate();
        }
        disp_ctrl.auto_refresh_time = 500;
        disp_ctrl.com_data.i_temp[0] = hr_get_hr_data();
        disp_ctrl.com_data.i_temp[1] = hr_if_band_wear();
        datetime_t UIdatetime = DateSync();
        disp_ctrl.com_data.i_temp[3] = UIdatetime.time.hour;
        if (if_time_hour_system_12())
        {
            disp_ctrl.com_data.i_temp[3] %= 12;
            if (disp_ctrl.com_data.i_temp[3] == 0)
            {
                disp_ctrl.com_data.i_temp[3] = 12;
            }
        }
        disp_ctrl.com_data.i_temp[4] = UIdatetime.time.minute;
        disp_ctrl.com_data.i_temp[8] = battery_ctrl_if_charging();
        disp_ctrl.com_data.i_temp[9] = battery_ctrl_get_percent();
        disp_ctrl.com_data.i_temp[5] = ble_if_connected();
    }

    view_blood_oxygen_animation();
    view_blood_oxygen_heart_reat();
    view_blood_oxygen_value();
    view_blood_oxygen_heart_reat_value();
    show_time_0_ble_with_xy(disp_ctrl.com_data.i_temp[5], 6, 100);
}

view_event_result_t view_blood_oxygen_event(int view, event_param_t const *param)
{
    switch (param->event)
    {
#ifdef EVENT_LEVEL_DOWN
    case event_move_level_down:
#endif
#ifdef EVENT_BUTTON_FRONT
    case event_button_front_long_press:
#endif
        return view_event_result_handled;
    case event_view_entry:
        LOG("view_blood_oxygen_event entry");
        disp_ctrl.auto_close_delay = 60000;
        hr_test_start(HR_CTRL_TYPE_HR_TEST);
        return view_event_result_handled;
    case event_view_re_screen_on:
        disp_ctrl.auto_close_delay = 60000;
        return view_event_result_handled;
    case event_view_exit:
        hr_test_stop(HR_CTRL_TYPE_HR_TEST);
        return view_event_result_handled;
    }
    return view_event_result_not_handled;
}
